﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MailService;
using MailService.Mime;
using MailService.Security;
using MailService.Pop3Mail;
using MailService.SmtpMail;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Configuration;

namespace Trade.Mail
{
    public class MailBox
    {


        public DataSet GetMailList()
        {
            DataSet dsMailList = new DataSet();
            string strServer="pop.163.com";
            int iServerPort=110;
            string strAccountName="18658122519@163.com";
            string strAccountPass="dxpeng333";

            //Pop3对象；收件箱处理程序对象；
            Pop3 pop = new Pop3();
            MailMessageCollection msgs = null;

            try
            {
                pop.Connect(strServer, iServerPort, true);
                AuthenticationMethods MoAuthMethods = AuthenticationMethods.Auto;
                pop.Login(strAccountName, strAccountPass, MoAuthMethods);


                if (pop.InboxMessageCount > 0)
                {
                    msgs = pop.DownloadMessageHeaders();
                    msgs.Reverse();
                }
                pop.Disconnect();

                DataTable dtMailList = new DataTable();
                dtMailList.Columns.Add("Attachment", typeof(string));
                dtMailList.Columns.Add("From", typeof(string));
                dtMailList.Columns.Add("To", typeof(string));
                dtMailList.Columns.Add("Subject", typeof(string));
                dtMailList.Columns.Add("Date", typeof(string));
                dtMailList.Columns.Add("Size", typeof(string));

                foreach (MailMessage msg in msgs)
                {
                    DataRow drRow = dtMailList.NewRow();
                    if (msg.HasAttachments)
                    {
                        drRow["Attachment"] = "有附件";
                    }
                    drRow["From"] = msg.From.AsString;
                    drRow["To"] = msg.To.AsString;
                    drRow["Subject"] = msg.Subject;
                    drRow["Date"] = msg.Date.ToString();
                    drRow["Size"] = msg.SizeOnServer.ToString();
                    dtMailList.Rows.Add(drRow);
                }
                dtMailList.TableName = "T_MailList";
                dsMailList.Tables.Add(dtMailList);
                return dsMailList;
            }
            catch (Exception ex)
            {
                return dsMailList;
            }
            return dsMailList;
        }

        public string SendMail(string strDirLog,string strMailTo,string MailCC,string strMailBcc,string strSubject,string strType,string strMailBody) 
        {
            string msg = string.Empty;
            string strServer = "smtp.163.com";
            string strAccountName = "18658122519@163.com";
            string strAccountPass = "dxpeng333";

            AuthenticationMethods[] authMethodArray =new AuthenticationMethods[] {
										AuthenticationMethods.None,
										AuthenticationMethods.Auto,
										AuthenticationMethods.SaslLogin,
										AuthenticationMethods.SaslCramMD5,
										AuthenticationMethods.SaslNtlm,
            };

            Smtp mailer = new Smtp();
            mailer.Log.Enabled = true;
            mailer.Log.Filename = strDirLog + "\\log.txt";
            mailer.Log.Format = LogFormatOptions.AddContextInfo;


            mailer.DnsServers.Clear();
            mailer.SmtpServers.Clear();
            mailer.SmtpServers.Add(
                    strServer,
                    strAccountName,
                    strAccountPass,
                    authMethodArray[2]
                    );
            // Set message headers.
            mailer.From.AsString = strAccountName;
            mailer.To.AsString = strMailTo;
            mailer.Cc.AsString = MailCC;
            mailer.Bcc.AsString = strMailBcc;
            mailer.Subject = strSubject;

            // Set message body.
            if (strType.Trim().ToLower()=="html".Trim().ToLower())
            {
                mailer.Message.BodyPlainText = string.Empty;
                mailer.Message.BodyHtmlText = strMailBody;
            }
            else
            {
                mailer.Message.BodyPlainText = strMailBody;
                mailer.Message.BodyHtmlText = string.Empty;
            }
            //Clear the log file before sending.
            mailer.Log.Clear();
            try
            {
                bool bo = mailer.Send();
                if (bo)
                {
                    if (mailer.GetRefusedRecipients().Count > 0)
                    {
                        msg = "The message was sent successfully,however those are not " + mailer.GetRefusedRecipients().ToString();
                    }
                    else
                    {
                        msg = "success";
                    }
                }
            }
            catch (Exception ex)
            {
                if (mailer.IsConnected)
                {
                    // This will disconnect on error.
                    try
                    {
                        mailer.Disconnect();
                    }
                    catch (MailServiceException)
                    {
                        msg = "error";
                    }
                }
            }
            return msg;
        }

    }
}
